# coding: UTF-8
import matplotlib.pyplot
import numpy
import os
import datetime


def get_path() -> str:
    current_path = os.path.abspath(__file__)
    return current_path


def get_filename(FP_path) -> list:
    """【函 数】批量获取文件名
    """
    # file_name = FP_path.split("\\")[-1]
    file_name = os.listdir(FP_path)
    return file_name


def read_data(FP_data_file) -> tuple:
    """【函 数】读取数据
    """
    # 以指定的格式读取文件的指定列
    VAR_data_type = numpy.dtype([("wavelength", "f"), ("intensity", "f")])
    # 按照指定的设置读取文件
    data_array = numpy.loadtxt(fname=FP_data_file, usecols=(0, 1), unpack=True)
    return data_array


def data_inf(FP_data):
    print("数据概览：", FP_data)
    print("读取格式：", type(FP_data))
    print("数组维度：", FP_data.shape)
    print("数据类型：", FP_data.dtype)


def save_image(FP_path: str) -> None:
    """【函 数】保存图片
    """
    matplotlib.pyplot.savefig(FP_path)
    return None


def save_txt(FP_path: str, FP_data: list) -> None:
    """【函 数】将处理后的数据保存为文本文件（.txt）
    """
    # numpy.savetxt(FP_path, FP_data, delimiter=" ")
    STR_time = get_time()
    with open(FP_path, "a") as output_file:
        output_file.write(STR_time + "\n")
        output_file.write(FP_data)
    return None


def get_time() -> str:
    """【函 数】获取当前时间
    """
    current_time = datetime.datetime.now()  # 【定义对象】
    str_time = datetime.datetime.strftime(current_time, "%Y.%m.%d %H:%M:%S")    # 格式化
    return str_time


if (__name__ == "__main__"):
    # VAR_data_file_1 = "C:\\Github\\Learn-Matplotlib\\spectrum\\data\\origin_data.txt"
    # data_1 = read_data(VAR_data_file_1)
    # data_inf(data_1)
    # # data_1.T
    # data_inf(data_1)
    # numpy.savetxt("converted_x.csv", (data_1[0], data_1[1]), fmt="%0.2f")

    data_path = r"C:\\Github\\Learn-Matplotlib\\spectrum\data\\"

    file_name = get_filename(data_path)
    print(type(file_name))
    print(file_name)
    print(get_time())
    STR_time = get_time()
    with open("output\\max_intensity.txt", "w") as output_file:
        output_file.write(STR_time)
